﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// DES(Data Encryption Standard)
    /// DES使用的密钥key为8字节，初始向量IV也是8字节。
    /// </summary>
    public class DESEncryptHelper
    {
        private string _Key;
        private string _IV;
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        public DESEncryptHelper(string key, string iv)
        {
            _Key = key;
            _IV = iv;
        }
        #endregion 
        #region  加密字符串
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="value"> 需要加密字符串</param>
        /// <returns> 加密字符串</returns>
        public string Encrypt(string value)
        {
            return Encrypt(value, _Key, _IV);
        }
        private string Encrypt(string encryptString, string key, string iv)
        {
            StringBuilder _encryptBuilder = new StringBuilder();
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] _bytes = Encoding.Default.GetBytes(encryptString);
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(iv);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(_bytes, 0, _bytes.Length);
                        cs.FlushFinalBlock();
                    }
                    foreach (byte b in ms.ToArray())
                        _encryptBuilder.AppendFormat("{0:X2}", b);
                }
            }
            return _encryptBuilder.ToString();
        }
        #endregion 
        #region 解密字符串
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="value">需要解密字符串</param>
        /// <returns> 解密字符串</returns>
        public string Decrypt(string value)
        {
            return Decrypt(value, _Key, _IV);
        }
        private string Decrypt(string encryptString, string key, string iv)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] _bytes = new byte[encryptString.Length / 2];
                for (int x = 0; x < encryptString.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(encryptString.Substring(x * 2, 2), 16));
                    _bytes[x] = (byte)i;
                }
                des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                des.IV = ASCIIEncoding.ASCII.GetBytes(iv);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        try
                        {
                            cs.Write(_bytes, 0, _bytes.Length);
                            cs.FlushFinalBlock();
                            return System.Text.Encoding.Default.GetString(ms.ToArray());
                        }
                        catch (CryptographicException)
                        {
                            return "N/A";
                        }
                    }
                }
            }
        }
        #endregion 
        #region 判断加密字符串和解密字符串是否相等
        /// <summary>
        /// 判断加密字符串和解密字符串是否相等
        /// </summary>
        /// <param name="EnString">加密字符串</param>
        /// <param name="DeString">解密字符串</param>
        /// <returns>bool</returns>
        public bool ValidateString(string EnString, string DeString)
        {
            return Decrypt(EnString, _Key, _IV).Equals(DeString.ToString());
        }
        #endregion 
    }
}
